define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    if(Config.virtualmachine == '' || Config.virtualmachine == null){
        Config.virtualmachine = 0;
    }
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'virtualmachinepcardapply/index' + location.search,
                    add_url: 'virtualmachinepcardapply/add/?virtualmachine=' + Config.virtualmachine,
                    edit_url: 'virtualmachinepcardapply/edit',
                    del_url: 'virtualmachinepcardapply/del',
                    multi_url: 'virtualmachinepcardapply/multi',
                    import_url: 'virtualmachinepcardapply/import',
                    table: 'virtualmachine_pcardapply',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'status desc,passingtime desc',
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'admin_id', title: __('Admin_id'),visible: false,addClass: "selectpage", extend: "data-source='auth/admin/index' data-field='nickname'"},
                        {field: 'admin_name', title: __('Admin_id'), operate: false},
                        {field: 'virtualmachine', title: __('Virtualmachine'),visible: false,addClass: "selectpage", extend: "data-source='feditionaccount/index' data-field='email'"},
                        {field: 'virtualmachine_name', title: __('Virtualmachine'), operate: false},
                        // {field: 'ip', title: __('IP'), operate: 'LIKE'},
                        // {field: 'ippassword', title: __('IP密码'), operate: 'LIKE'},
                        // {field: 'email', title: __('邮箱'), operate: 'LIKE'},
                        // {field: 'emailpassword', title: __('邮箱密码'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'passingtime', title: __('通过审核时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('申请通过'),"hidden":__('申请中'),"refuse":__('拒绝')}, formatter: Table.api.formatter.status,custom: {'Normal':'success', 'hidden':'warning', 'refuse':'danger'}},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                $("#znum").text(data.Statistics.znum);
                $("#ztoday").text(data.Statistics.ztoday);
                $("#nohandle").text(data.Statistics.nohandle);
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $("#c-virtualmachine").data("formatItem",function (item) {
                return "账户编号:"+item.number+","+item.numbername_email;
            });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                require(['selectpage'], function (SelectPage) {
                    var _displayResults = $.fn.selectPage.Constructor.prototype.displayResults;
                    $.fn.selectPage.Constructor.prototype.displayResults = function (self, json, is_query) {
                        _displayResults.call(this, self, json, is_query);
                        if (self.elem.hidden.is("#c-pcard")) {
                            $(self.elem.results[0]).append("<p class='text-center' style='margin:5px;'><a class='btn btn-xs btn-info btn-dialog' title='新增P卡账户' data-window='top' href='virtualmachinepcard/add'>新增P卡账户</a></p><p style='margin:5px;'>（新增P卡账户后需要取消下拉框，重新选择）</p>");
                        }
                    };
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});